package repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.TermModel;

import entity.BusiestCluster;
import entity.Term;

public class TermRepository {

	private ClusterRepository clusterRepository;

	public TermRepository() {

		this.clusterRepository = new ClusterRepository();

	}

	/**
	 * Check if is first access.
	 * 
	 * @return
	 * @throws ClassNotFoundException
	 *             invalid driver.
	 * @throws SQLException
	 *             invalid connection.
	 */
	public boolean checkFirstAcess() throws ClassNotFoundException,
			SQLException {

		if (clusterRepository.countCluster() == 0) {

			// is first access.
			return true;

		} else {

			// not first access.
			return false;
		}

	}

	/**
	 * get all term of a particula cluster.
	 * 
	 * @param idCluster
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public List<Term> getTerms(int idCluster) throws ClassNotFoundException,
			SQLException {

		List<Term> terms = new ArrayList<>();

		// Open connection
		Connection connection = FactoryConnection.getInstance().getConnection();

		Statement statement = connection.createStatement();

		ResultSet resultClusters = statement
				.executeQuery("SELECT * FROM term WHERE cluster_id_cluster = "
						+ idCluster + "");

		while (resultClusters.next()) {

			Term term = new Term(resultClusters.getInt("id_term"),
					resultClusters.getInt("cluster_id_cluster"),
					resultClusters.getString("term"),
					resultClusters.getString("new_term"));
			terms.add(term);

		}

		return terms;
	}

	public void saveTerm(List<Term> termsParam, String query,
			String askQueryString) throws ClassNotFoundException, SQLException {

		List<BusiestCluster> idClusters = clusterRepository
				.getClustersBusiest();

		Connection connection = FactoryConnection.getInstance().getConnection();

		Statement statement = connection.createStatement();

		TermModel termModel = new TermModel();

		boolean isIdentical = false;

		if (checkFirstAcess()) {

			clusterRepository.createCluster(termsParam, askQueryString);
			clusterRepository.createBusiestCluster(clusterRepository
					.getLastCluster());

		} else {

			// get all busiest cluster
			for (BusiestCluster busiestCluster : idClusters) {

				List<Term> termsRepository = getTerms(busiestCluster.getId());

				double score = termModel.checkSimilarQuery(termsParam,
						termsRepository);

				if (score == 1.000) {

					System.out.println("Consulta 100% similar!");
					clusterRepository.updateBusiestCluster(
							busiestCluster.getIdCluster(),
							busiestCluster.getId());
					return;
				} else if (score >= 0.600) {

					for (Term termParam : termsParam) {

						// get all term of a param
						for (Term termRepository : termsRepository) {

							// Insere os term na tabela de TERM
							if (termParam.getTerm().equals(
									termRepository.getTerm())) {

								isIdentical = false;

								statement
										.executeUpdate("DELETE FROM term WHERE id_term = "
												+ termRepository.getId() + "");

								statement
										.executeUpdate("INSERT INTO term (cluster_id_cluster, term) VALUES("
												+ busiestCluster.getIdCluster()
												+ ",'"
												+ termParam.getTerm()
												+ "');");

								break;
							} else {

								isIdentical = false;
							}

						}

						// Insere os termos na lista auxiliar(new_term)
						if (isIdentical == false) {

							statement
									.executeUpdate("INSERT INTO term (cluster_id_cluster, term, new_term) VALUES("
											+ busiestCluster.getIdCluster()
											+ ",'"
											+ termParam.getTerm()
											+ "','"
											+ termParam.getTerm()
											+ "');");

						}
					}

				} else {

					// consulta ask
					// criar um cluster
					clusterRepository.createCluster(termsParam, askQueryString);
					return;
				}

				clusterRepository.createBusiestCluster(clusterRepository
						.getLastCluster());

			}

		}

	}

}
